import { Router, IRouter } from 'express';
import {
  getLocations,
  getLocationById,
  createLocation,
  updateLocation,
  deleteLocation,
  approveLocation,
  rejectLocation
} from '../controllers/location.controller';
import {
  rateLocation,
  getMyRating,
  deleteRating
} from '../controllers/rating.controller';
import {
  getLocationDishes,
  addRecommendedDish,
  updateRecommendedDish,
  deleteRecommendedDish
} from '../controllers/dish.controller';
import {
  getLocationCongestion,
  updateCongestionLevel,
  batchUpdateCongestion,
  deleteCongestionLevel
} from '../controllers/congestion.controller';
import { authenticateToken, requireAdmin } from '../middleware/auth';
import { uploadDishImage } from '../utils/uploadConfig';

const router: IRouter = Router();

/**
 * @route   GET /api/v1/locations
 * @desc    获取地点列表
 * @access  Public
 * @query   status?: PENDING|APPROVED|REJECTED, page?: number, limit?: number
 */
router.get('/', getLocations);

/**
 * @route   GET /api/v1/locations/:id
 * @desc    获取地点详情
 * @access  Public
 */
router.get('/:id', getLocationById);

/**
 * @route   POST /api/v1/locations
 * @desc    创建地点（管理员自动通过，普通用户需审核）
 * @access  Private (需要JWT认证)
 * @body    { name: string, coordinateX: number, coordinateY: number }
 */
router.post('/', authenticateToken, createLocation);

/**
 * @route   PUT /api/v1/locations/:id
 * @desc    更新地点信息
 * @access  Private (需要JWT认证，仅创建者或管理员)
 * @body    { name?: string, coordinateX?: number, coordinateY?: number }
 */
router.put('/:id', authenticateToken, updateLocation);

/**
 * @route   DELETE /api/v1/locations/:id
 * @desc    删除地点
 * @access  Private (需要JWT认证，仅创建者或管理员)
 */
router.delete('/:id', authenticateToken, deleteLocation);

/**
 * @route   PUT /api/v1/locations/:id/approve
 * @desc    审核通过地点
 * @access  Private (需要JWT认证和管理员权限)
 */
router.put('/:id/approve', authenticateToken, requireAdmin, approveLocation);

/**
 * @route   PUT /api/v1/locations/:id/reject
 * @desc    拒绝地点
 * @access  Private (需要JWT认证和管理员权限)
 */
router.put('/:id/reject', authenticateToken, requireAdmin, rejectLocation);

/**
 * @route   POST /api/v1/locations/:id/ratings
 * @desc    为地点评分（创建或更新）
 * @access  Private (需要JWT认证)
 * @body    { score: number } (0-5分)
 */
router.post('/:id/ratings', authenticateToken, rateLocation);

/**
 * @route   GET /api/v1/locations/:id/ratings/my
 * @desc    获取我对该地点的评分
 * @access  Private (需要JWT认证)
 */
router.get('/:id/ratings/my', authenticateToken, getMyRating);

/**
 * @route   DELETE /api/v1/locations/:id/ratings
 * @desc    删除我的评分
 * @access  Private (需要JWT认证)
 */
router.delete('/:id/ratings', authenticateToken, deleteRating);

/**
 * @route   GET /api/v1/locations/:id/dishes
 * @desc    获取地点的推荐菜品列表
 * @access  Public
 */
router.get('/:id/dishes', getLocationDishes);

/**
 * @route   POST /api/v1/locations/:id/dishes
 * @desc    添加推荐菜品（支持图片上传）
 * @access  Private (需要JWT认证)
 * @body    FormData { dishName: string, floor?: string, price?: number, image?: File }
 */
router.post('/:id/dishes', authenticateToken, uploadDishImage.single('image'), addRecommendedDish);

/**
 * @route   PUT /api/v1/locations/:locationId/dishes/:dishId
 * @desc    更新推荐菜品（支持图片上传）
 * @access  Private (需要JWT认证，仅创建者或管理员)
 * @body    FormData { dishName?: string, floor?: string, price?: number, image?: File }
 */
router.put('/:locationId/dishes/:dishId', authenticateToken, uploadDishImage.single('image'), updateRecommendedDish);

/**
 * @route   DELETE /api/v1/locations/:locationId/dishes/:dishId
 * @desc    删除推荐菜品
 * @access  Private (需要JWT认证，仅创建者或管理员)
 */
router.delete('/:locationId/dishes/:dishId', authenticateToken, deleteRecommendedDish);

/**
 * @route   GET /api/v1/locations/:id/congestion
 * @desc    获取地点的拥堵程度
 * @access  Public
 */
router.get('/:id/congestion', getLocationCongestion);

/**
 * @route   PUT /api/v1/locations/:id/congestion
 * @desc    更新地点的拥堵程度（单个时段）
 * @access  Private (需要JWT认证)
 * @body    { timePeriod: TimePeriod, level: number } (level: 1-5)
 */
router.put('/:id/congestion', authenticateToken, updateCongestionLevel);

/**
 * @route   PUT /api/v1/locations/:id/congestion/batch
 * @desc    批量更新地点的拥堵程度
 * @access  Private (需要JWT认证)
 * @body    { congestions: [{ timePeriod: TimePeriod, level: number }] }
 */
router.put('/:id/congestion/batch', authenticateToken, batchUpdateCongestion);

/**
 * @route   DELETE /api/v1/locations/:id/congestion/:timePeriod
 * @desc    删除地点某时段的拥堵程度
 * @access  Private (需要JWT认证和管理员权限)
 */
router.delete('/:id/congestion/:timePeriod', authenticateToken, requireAdmin, deleteCongestionLevel);

export default router;
